package pd.struct.statement;

import java.util.ArrayList;
import java.util.Collection;

/**
 * A StatementObject represents a single statement and its parameters.
 *
 * @author Joshua Swank
 */
public class StatementObject implements Cloneable
{
    /**
     * The symbol representing the statement's type. Can be null.
     */
    private StatementType symbol;

    /**
     * The parameters of the statement.
     */
    private final ArrayList<Object> params = new ArrayList<Object>();

    /**
     * Initializes a statement object with the specified symbol.
     *
     * @param symbol the new statement's symbol.
     */
    public StatementObject( StatementType symbol )
    {
        this.symbol = symbol;
    }

    /**
     * Initializes a statement object with the specified symbol and parameters.
     *
     * @param symbol the new statement's symbol.
     * @param parameters the new statement's parameters.
     */
    public StatementObject( StatementType symbol, Collection<Object> parameters )
    {
        this.symbol = symbol;

        if( parameters != null )
            params.addAll( parameters );
    }

    /**
     * Returns a new StatementObject with a shallow clone of the parameter list.
     *
     * @return a clone of the StatementObject
     */
    public StatementObject clone()
    {
        return new StatementObject( symbol, params );
    }

    /**
     * Returns the statement's symbol.
     *
     * @return the statement's symbol.
     */
    public StatementType getSymbol()
    {
        return symbol;
    }

    /**
     * Compares two statements and returns true if they have the same symbol.
     *
     * @param statement the statement to compare to.
     * @return true if the two statements have the same symbol, false if they have different symbols.
     */
    public boolean sameType( StatementObject statement )
    {
        if( statement != null )
                return ( symbol == statement.symbol );

        return false;
    }

    /**
     * Returns true if the two statements have the same symbol and same parameters.
     *
     * @param statement the statement to compare to.
     * @return true if the two statements have the same symbol and parameters, false otherwise.
     */
    public boolean equals( StatementObject statement )
    {
        if( sameType( statement ) && statement.params.size() == params.size())
        {
            for( int i = 0; i < params.size(); i++ )
                if( !params.get( i ).equals( statement.params.get( i )) )
                    return false;

            return true;
        }
        else
            return false;
    }

    /**
     * Represents the statement as a string.
     *
     * @return the statement's symbol as a string.
     */
    public String toString()
    {
        if( symbol != null )
            return symbol.toString( params );
        else
            return "No symbol";
    }
}